﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;     // Controls;
using UCore;

namespace Twin {
    public class UI {


        public static string init_cmbFile(ComboBox cmbFiles, string arg) {
            int i;
            for (i = 0; i < Parsing.area_name.Length; i++) {
                cmbFiles.Items.Add(Parsing.area_name[i]);
            }

            cmbFiles.Items.Add("_MASS");
            cmbFiles.Items.Add("_IAP");

            string s = Common.RemoveBracketedContent(arg);
            i = int.Parse(s);
            cmbFiles.SelectedIndex = i;
            return s;
        }


        public static string init_cmbCmfg(ComboBox cmbCmfg, string arg) {
            cmbCmfg.Items.Add("空");

            int i;
            for (i = 0; i < Ini.size_of.cmfg; i++) {
                cmbCmfg.Items.Add(i.ToString());
            }

            string s = Common.RemoveBracketedContent(arg);
            //arg[INST_CMFG] = s;

            if (s.Equals("")) {
                cmbCmfg.SelectedIndex = 0;
            } else {
                i = int.Parse(s);
                cmbCmfg.SelectedIndex = i + 1;
            }
            return s;
        }


        public static void init_cmbCom(ComboBox cmbCom, int default_port) {
            int i;
            string COMn;
            for (i = 0; i < Ini.size_of.port; i++) {
                COMn = "com" + (i + 1).ToString();
                cmbCom.Items.Add(COMn);
            }
            cmbCom.SelectedIndex = default_port;   // COM4;
        }


        public static int init_cmbChipSel(ComboBox cmbChip, Chip[] chips) {
            for (int i = 0; i < chips.Count(); i++) {
                cmbChip.Items.Add(chips[i].name);
            }
            cmbChip.SelectedIndex = 0;
            return cmbChip.SelectedIndex;
        }


        public static int init_cmbFileSel(ComboBox cmbFile, List<TestFile> tf) {
            for (int i = 0; i < tf.Count(); i++) {
                cmbFile.Items.Add(tf[i].fn);
            }
            cmbFile.SelectedIndex = 0;
            return cmbFile.SelectedIndex;
        }


        public static void init_cmbSegSize(ComboBox cmbSegSize, Chip chip) {
            //for (int i = 0; i < Parsing.seg_size.Length; i++) {
            //    cmbSegSize.Items.Add(Parsing.seg_size[i]);
            //}
            for (int i = 0; i < chip.seg_size.Count; i++) {
                cmbSegSize.Items.Add(chip.seg_size[i]);
            }
            cmbSegSize.SelectedIndex = 2;
        }


        public static void init_cmbIFmt(ComboBox cmbIFmt, int iFmt) {
            for (int i = 0; i < Ini.size_of.fmt; i++) {
                cmbIFmt.Items.Add(i.ToString());
            }
            cmbIFmt.SelectedIndex = iFmt;
        }


        public static string init_cmbArea(ComboBox cmbArea, string arg) {
            //cmbArea
            int i;
            for (i = 0; i < Parsing.area_name.Length; i++) {
                cmbArea.Items.Add(Parsing.area_name[i]);
            }

            //初始化
            string s = Common.RemoveBracketedContent(arg);   //"10"
            int k = int.Parse(s);
            cmbArea.SelectedIndex = k;

            //arg[INST_VM_AREA] = s;
            return s;
        }


        public static string init_cmbVmArea(ComboBox cmbVM, ComboBox cmbArea, string arg) {
            //cmbVM
            if (cmbVM != null) {
                cmbVM.Items.Clear();
                cmbVM.Items.Add("v");
                cmbVM.Items.Add("m");
            }

            //cmbArea
            int i;
            for (i = 0; i < Parsing.area_name.Length; i++) {
                cmbArea.Items.Add(Parsing.area_name[i]);
            }

            //初始化
            string s = Common.RemoveBracketedContent(arg);   //"10"
            int j = int.Parse(s.Substring(0, 1));
            cmbVM.SelectedIndex = j;

            int k = int.Parse(s.Substring(1, 1));
            cmbArea.SelectedIndex = k;

            //arg[INST_VM_AREA] = s;
            return s;
        }


        //cmbBloc
        public static string init_cmbBloc(ComboBox cmbBloc) {
            cmbBloc.Items.Clear();
            cmbBloc.Items.Add("iap bank0");
            cmbBloc.Items.Add("iap bank1");

            cmbBloc.SelectedIndex = 0;
            string s = cmbBloc.SelectedItem.ToString();
            
            return s;
        }


        //赋值cmbXcx， 初始化Xme
        public static int init_cmbXcx(ComboBox cmbXcx) {
            string label;
            int num = 0;
            for (int iCx = 0; iCx < Ini.size_of.elem; iCx++) {
                label = "root.Els.Elem[" + iCx.ToString() + "].structPP.bXME";   //=0
                string sXME = Common.getAbsV(label, Parsing.map_abs["absROOT"]);
                if(sXME.Equals("0"))continue;

                //add Name
                label = "root.Els.Elem[" + iCx.ToString() + "].Name";
                string s = Common.getAbsV(label, Parsing.map_abs["absROOT"]);
                cmbXcx.Items.Add(s);

                //lcx.Cx[4].structPATITION.Year = 0x19 >
                //lcx.Cx[4].structPATITION.Month = 0x01 >
                //lcx.Cx[4].structPATITION.Day = 0x01 >
                //lcx.Cx[4].structPATITION.Hour = 0x08 >
                //lcx.Cx[4].structPATITION.Duration = 0x01 >
                //lcx.Cx[4].structPATITION.Unit = 0x00



                num++;
            }
            cmbXcx.SelectedIndex = 0;





            return num;
        }


        /*public static void init_cmbMiscInst(ComboBox cmbMiscInst, string[] inst) {
            int i;
            for (i = 0; i < Parsing.port_size; i++) {
                cmbMiscInst.Items.Add(inst[i]);
            }
            cmbMiscInst.SelectedIndex = 0;
        }*/


        public static void init_dgvGI(DataGridView dgvGI, int iFmt) {
            dgvGI.Rows.Clear();

            string label;
            label = "root.Bcfg.Belt[" + iFmt.ToString() + "].Gi";
            string sInstant_r = Common.getAbsV(label, Parsing.map_abs["absROOT"]);
            string[] s = sInstant_r.Split(',');
            string sInstant = s[0];

            byte[] iElem = Common.Hex2Array(sInstant);

            for (int i = 0; i < iElem.Length; i++) {
                label = "root.Els.Elem[" + iElem[i].ToString() + "].Name";
                string e_name = Common.getAbsV(label, Parsing.map_abs["absROOT"]);

                // 将选中的项添加到DataGridView的尾部
                int newRow = dgvGI.Rows.Add(e_name);
                dgvGI.Rows[newRow].Cells[1].Value = ((uint)iElem[i]).ToString("X2"); // "X2" 格式化为两位16进制

                // 设置新行的第一列为选中状态
                //dgvGI.CurrentCell = dgvGI.Rows[newRow].Cells[0];
            }
        }


        public static void init_dgvInstant(DataGridView dgvInstant, string sInstant) {
            string label;
        
            byte[] iElem = Common.Hex2Array(sInstant);

            for (int i = 0; i < iElem.Length; i++) {
                label = "root.Els.Elem[" + iElem[i].ToString() + "].Name";
                string e_name = Common.getAbsV(label, Parsing.map_abs["absROOT"]);

                // 将选中的项添加到DataGridView的尾部
                int newRow = dgvInstant.Rows.Add(e_name);
                dgvInstant.Rows[newRow].Cells[1].Value = ((uint)iElem[i]).ToString("X2"); // "X2" 格式化为两位16进制

                // 设置新行的第一列为选中状态
                //dgvGI.CurrentCell = dgvGI.Rows[newRow].Cells[0];
            }
        }


        public static void init_listEls(ListBox listEls) {
            for (int i = 0; i < Ini.size_of.elem; i++) {
                string label = "root.Els.Elem[" + i.ToString() + "].Name";
                string s = Common.getAbsV(label, Parsing.map_abs["absROOT"]);
                listEls.Items.Add(s);
            }
            listEls.SelectedIndex = 0;
        }


        public static void init_listAbs(ListBox listAbs, int iArea) {
            string absName = "abs" + Parsing.area_name[iArea];
            string[] abs = Parsing.map_abs[absName].Split('>');

            Array.Sort(abs);

            // 将字符串数组转换为List<string>，因为ComboBox的DataSource需要一个集合
            List<string> absList = new List<string>(abs);

            // 加载到ListBox
            listAbs.Items.Clear(); // 清空ListBox中的所有项目
            foreach (string item in absList) {
                listAbs.Items.Add(item); // 将每个项目添加到ListBox
            }

            // 如果需要，可以设置ListBox的Sorted属性为true，以便自动排序
            listAbs.Sorted = true;
        }






    }
}
